import jieba

with open("test.txt", "rb") as f:
    text = f.read()
    words = jieba.cut(text)

word_count = {}
for word in words:
    if len(word) > 1:
        word_count[word] = word_count.get(word, 0) + 1

top_words = sorted(word_count.items(), key=lambda x: x[1], reverse=True)[:5]

with open("output.txt", "w", encoding="utf-8") as f:
    f.write("---出现次数前5的词汇及出现次数---\n")
    for word, count in top_words:
        f.write(f"{word}: {count}\n")
